<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台（PHP版）
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | 开源版本可自由商用，可去除界面版权logo
// | gitee下载：https://gitee.com/likeshop_gitee/likeadmin
// | github下载：https://github.com/likeshop-github/likeadmin
// | 访问官网：https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------

namespace app\api\logic;

use app\admin\model\Ad;
use app\admin\model\Article;
use app\admin\model\Projects;
use app\common\enum\ProjectEnum;
use app\common\logic\BaseLogic;
use app\common\service\ConfigService;
use app\common\service\FileService;

/**
 * index
 * Class IndexLogic
 * @package app\api\logic
 */
class IndexLogic extends BaseLogic
{

    /**
     * @notes 首页数据
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getIndexData()
    {
        $data = [];
        $data['notices'] = Article::where(['cate_id' => 1, 'status' => 1, 'delete_time' => null])
            ->order('sort', 'desc')
            ->limit(5)
            ->select()
            ->toArray();

        $data['activities'] = Article::with(['village','cate'])->where(['cate_id' => 3, 'status' => 1, 'delete_time' => null])
            ->order('sort', 'desc')
            ->limit(2)
            ->select()
            ->toArray();

        $data['banner'] = Ad::where(['type_id' => 1, 'status' => 1])
            ->order('sort', 'desc')
            ->limit(5)
            ->select()
            ->toArray();

        $data['key_banner'] = Ad::where(['type_id' => 2, 'status' => 1])
            ->order('sort', 'desc')
            ->limit(1)
            ->select()
            ->toArray();
        $data['index_bg'] = ConfigService::get('site', 'index_bg', 'static/images/index_bg.png');
        $data['nav'] = '[{"title":"导航菜单","name":"nav","content":{"enabled":1,"data":[{"image":"/static/images/icon/policy.png","name":"政策","link":{"path":"/pages/news/news?cat_id=2","name":"政策","type":"shop"},"is_show":"1"},{"image":"/static/images/icon/home_power.png","name":"家力量","link":{"path":"/pages/project/projects","name":"家力量","type":"shop","canTab":"true"},"is_show":"1"},{"image":"/static/images/icon/talent_pool.png","name":"人才库","link":{"path":"/pages/talent/talent","name":"人才库","type":"shop"},"is_show":"1"},{"image":"/static/images/icon/activity.png","name":"活动","link":{"path":"/pages/news/news?cat_id=3","name":"活动","type":"shop"},"is_show":"1"},{"image":"/static/images/icon/project.png","name":"重点项目","link":{"path":"/pages/key_projects/key_projects","name":"重点项目","type":"shop","canTab":"true"},"is_show":"1"},{"image":"/static/images/icon/join.png","name":"加入帮扶","link":{"path":"/pages/join/join","name":"加入帮扶","type":"shop"},"is_show":"1"}],"style":2,"per_line":3,"show_line":2},"styles":{}}]
     ';
        $data['index_total'] = [
            'publish' => '123456',
            'help' => '12345',
            'complete' => '123456',
            'talent' => '123456',
            'bg_image' => '/static/images/index_total_bg.png'
        ];
        return $data;
    }

    public static function getProjects(string $type)
    {
        $where = [
            'status' => 1,
            'delete_time' => null,
            'approve_status' => 2,
        ];

        switch ($type) {
            case '1':
                $where['project_status'] = ProjectEnum::SEEKING_HELP;
                break;
            case '2':
                $where['project_status'] = ProjectEnum::HELPING;
                break;
            case '3':
                $where['project_status'] = ProjectEnum::COMPLETED;
                break;
            default:
                break;
        }

        $projects = Projects::field('id,cate_id,title,describe,images,contact,village_id,likes,hits,approve_status,project_status,status,create_time,delete_time')
            ->with(['village','cate'])
            ->where($where)
            ->limit(5)
            ->select()
            ->order('create_time  desc')
            ->toArray();

        foreach ($projects as &$project) {
            $images = explode('|', $project['images']);
            $project['images'] = FileService::getFileUrl($images[0]);
            $project['village_name'] = $project['village']['name'];
            $project['create_time'] =  substr( $project['create_time'],0, 9);
            $project['cate_name'] = $project['cate']['title'];
        }
        return $projects;

    }


    /**
     * @notes 获取政策协议
     * @param string $type
     * @return array
     */
    public static function getPolicyByType(string $type)
    {
        return [
            'title' => ConfigService::get('agreement', $type . '_title', ''),
            'content' => ConfigService::get('agreement', $type . '_content', ''),
        ];
    }


    /**
     * @notes 装修信息
     * @param $id
     * @return array
     * @author 段誉
     * @date 2022/9/21 18:37
     */
    public static function getDecorate($id)
    {
        return DecoratePage::field(['type', 'name', 'data', 'meta'])
            ->findOrEmpty($id)->toArray();
    }


    /**
     * @notes 获取配置
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author 段誉
     * @date 2022/9/21 19:38
     */
    public static function getConfigData()
    {
        // 网址信息
        $website = [
            'web_name' => ConfigService::get('site', 'site_name'),
            'web_logo' => FileService::getFileUrl(ConfigService::get('site', 'logo_image')),
            'web_tel' => ConfigService::get('site', 'site_contact'),
        ];
        // H5配置
        $webPage = [
            // 渠道状态 0-关闭 1-开启
            'status' => ConfigService::get('web_page', 'status', 1),
            // 关闭后渠道后访问页面 0-空页面 1-自定义链接
            'page_status' => ConfigService::get('web_page', 'page_status', 0),
            // 自定义链接
            'page_url' => ConfigService::get('web_page', 'page_url', ''),
            'url' => request()->domain() . '/mobile'
        ];

        // 备案信息
        $copyright = ConfigService::get('copyright', 'config', []);

        return [
            'domain' => FileService::getFileUrl(),
            'website' => $website,
            'webPage' => $webPage,
            'version' => config('project.version'),
            'copyright' => $copyright,
        ];
    }

}